package com.hyhy.hycloud.commons.util.encrydecry;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hyhy.hycloud.commons.util.encrydecry.rsa.Des;
import com.hyhy.hycloud.commons.util.encrydecry.rsa.RsaEncrypt;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @ClassName: EncodEncryUtil
 * @Description: 加密解密编码
 * @author MS
 * @date 2018年2月6日
 */
public class EncodEncryUtil {

	private static final Logger logger = LoggerFactory.getLogger(EncodEncryUtil.class);

	public static String md5Digest(String src) throws Exception {
		MessageDigest md = MessageDigest.getInstance("MD5");
		byte[] b = md.digest(src.getBytes("UTF-8"));
		return byte2HexStr(b);
	}

	private static String byte2HexStr(byte[] b) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; ++i) {
			String s = Integer.toHexString(b[i] & 0xFF);
			if (s.length() == 1) {
				sb.append("0");
			}
			sb.append(s.toUpperCase());
		}
		return sb.toString();
	}

	/***
	 * MD5加码 生成32位md5码
	 */
	public static String string2MD5(String inStr) {
		if (StringUtils.isBlank(inStr)) {
			return "";
		}
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			e.printStackTrace();
			return inStr;
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	/**
	 * MD5加码 生成十六进制字符串md5码
	 */
	public static String md5Hex(String src) {
		if (StringUtils.isBlank(src)) {
			return "";
		}
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] bs = md5.digest(src.getBytes());
			return new String(new Hex().encode(bs));
		} catch (Exception e) {
			logger.error("错误信息：", e);
			return null;
		}
	}

	/**
	 * 加密解密算法 执行一次加密，两次解密
	 */
	public static String convertMD5(String inStr) {
		if (StringUtils.isBlank(inStr)) {
			return "";
		}
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;

	}

	public static String base64Encoder(String src) throws Exception {
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(src.getBytes("UTF-8"));
	}

	public static String base64Decoder(String dest) throws Exception {
		BASE64Decoder decoder = new BASE64Decoder();
		return new String(decoder.decodeBuffer(dest), "UTF-8");
	}

	/**
	 * base64编码
	 * 
	 * @param data
	 * @return 编码后的字符串
	 */
	public static String base64CodeData(String data) {
		if (StringUtils.isBlank(data)) {
			return "";
		}
		try {
			byte[] encodeBase64 = Base64.encodeBase64(data.getBytes("UTF-8"));
			return new String(encodeBase64);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		}
		return data;
	}

	/**
	 * base64解码
	 * 
	 * @param data
	 * @return 解码后的字符串
	 */
	public static String base64DecodeData(String data) {
		if (StringUtils.isBlank(data)) {
			return "";
		}
		try {
			byte[] decodeBase64 = Base64.decodeBase64(data.getBytes("UTF-8"));
			return new String(decodeBase64);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		}
		return data;
	}

	/**
	 * des加密字符串
	 * 
	 * @param encryptKey
	 * @param encryptString
	 * @return des加密后的字符串
	 * @throws Exception
	 */
	public static String encryptDES(String encryptString, String encryptKey)
			throws Exception {
		if (StringUtils.isBlank(encryptString)) {
			return "";
		}
		try {
			String encode = Des.encryptDES(encryptString, encryptKey);
			return encode;
		} catch (Exception ex) {
			throw new Exception("DES加密错误");
		}
	}

	/**
	 * des解密字符串
	 * 
	 * @param decryptString
	 * @param decryptKey
	 * @return des解密后的字符串
	 * @throws Exception
	 */
	public static String decryptDES(String decryptString, String decryptKey)
			throws Exception {
		if (StringUtils.isBlank(decryptString)) {
			return "";
		}
		try {
			String data = Des.decryptDES(decryptString, decryptKey);
			return String.valueOf(data);
		} catch (Exception ex) {
			throw new Exception("DES解密错误");
		}
	}

	/**
	 * 使用RSA加密字符串
	 * 
	 * @param source
	 * @return RSA加密后的字符串
	 */
	public static String encryptRSA(String source) throws Exception {
		if (StringUtils.isBlank(source)) {
			return "";
		}

		return RsaEncrypt.encryptWithBase64(source);
	}

	/**
	 * 使用RSA解密字符串
	 * 
	 * @param source
	 * @return RSA解密后的字符串
	 */
	public static String decryptRSA(String source) throws Exception {
		if (StringUtils.isBlank(source)) {
			return "";
		}

		return RsaEncrypt.decryptWithBase64(source);
	}

	/**
	 * 生成含有随机盐的密码 ,返回值为盐+盐密码
	 */
	public static Map<String, String> generateSalt(String password) {
		password = string2MD5(password);
		String salt = RandomStringUtils.randomAlphanumeric(32);
		password = md5Hex(password + salt);
		Map<String, String> map = new HashMap<String, String>();
		map.put("salt", salt);
		map.put("saltpassword", password);
		return map;
	}

}
